<!--  -->
<template>
  <div class="dialog-editor-menu-item">
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px"  :append-to-body="true" @closed="close" :close-on-click-modal="false">
      <el-form :model="form" v-if="dialogFormVisible" @submit.native.prevent :label-width="formLabelWidth" class="editor-menu-item">
        <el-form-item label="菜单名称" style="margin-bottom: 8px;" :error="textErrorMessage">
          <el-input v-model="form.text" autocomplete="off" size="mini"
            style="width:400px;"></el-input>
        </el-form-item>
        <el-form-item label="菜单级别" style="margin-bottom: 8px;">
          <el-select v-model="form.level" placeholder="选择级别" size="small" style="width:400px;">
            <el-option label="顶级" :value="-1"></el-option>
            <template v-for="(item, index) in list">
              <el-option :key="index" :label="item.text" :value="index" v-if="!disabledselect[index]"></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="链接地址" style="margin-bottom: 8px;" :error="hrefErrorMessage">
          <div>
            <el-input v-model="form.href" autocomplete="off" size="mini"
              style="width:400px;"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="打开方式" style="margin-bottom: 8px;">
          <div>
            <el-radio-group v-model="form.target" size="mini">
              <el-radio :label="'_self'">当前窗口</el-radio>
              <el-radio :label="'_blank'">新开窗口</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array
    }
  },
  created () {
    this.list.forEach((item, i) => {
      this.disabledselect[i] = false
    })
  },
  data () {
    return {
      dialogFormVisible: false,
      form: {
        text: '',
        href: '',
        target: '_self',
        level: -1,
        cur: false
      },
      formLabelWidth: '120px',
      title: '',
      callback: () => { },
      // 校验菜单名称错误信息
      textErrorMessage: null,
      // 校验链接错误信息
      hrefErrorMessage: null,
      disabledselect: []

    }
  },
  watch: {
  },
  methods: {
    // ....
    open (title, dataItem, index, callback) {
      this.title = title
      this.callback = callback
      this.form = JSON.parse(JSON.stringify(dataItem))
      if (dataItem.children) {
        this.list.forEach((item, i) => {
          this.disabledselect[i] = true
        })
      }
      if (index >= 0 && dataItem.level === -1) {
        this.disabledselect[index] = true
      }
      this.dialogFormVisible = true
    },
    close () {
      this.title = ''
      this.callback = () => { }
      this.form = {
        text: '',
        href: '',
        target: '_self',
        level: -1,
        cur: false
      }
      this.disabledselect = []
      this.hrefErrorMessage = null
      this.textErrorMessage = null
      this.dialogFormVisible = false
    },
    submit () {
      this.hrefErrorMessage = null
      this.textErrorMessage = null
      if (!this.form.text) {
        this.textErrorMessage = '请输入菜单名称！'
        return
      }
      if (!this.form.href) {
        // 需要输入链接的情况下，链接地址为空时
        this.hrefErrorMessage = '请输入链接地址！'
        return
      }
      // 对链接地址的验证
      let bool = /^(\/|\/\/|http:\/\/|https:\/\/).*?/.test(this.form.href)
      if (!bool) {
        this.hrefErrorMessage = '请输入正确链接地址!'
        return
      }
      this.callback(this.form)
      this.close()
    }
  }
}

</script>
<style scoped lang="scss">
.editor-menu-item /deep/ .el-form-item__error{
  padding-top:0;
  }
</style>
